﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Win32;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.InteropServices;


namespace DevelopStudio
{
    static class Program
    {
        [DllImport("User32.dll")]
        private static extern bool ShowWindowAsync(System.IntPtr hWnd, int cmdShow);
        [DllImport("User32.dll")]
        private static extern bool SetForegroundWindow(System.IntPtr hWnd);

        private const int WS_SHOWNORMAL = 1;

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            System.Security.Principal.WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent();
            System.Security.Principal.WindowsPrincipal principal = new System.Security.Principal.WindowsPrincipal(identity);

            // Check whether current user is administrator or not.
            if (principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator) == false)
            {
                // Create start info.
                System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();

                // Set start file name.
                startInfo.FileName = System.Windows.Forms.Application.ExecutablePath;

                // Use Administrator to run.
                startInfo.Verb = "runas";

                // If not administrator, use UAC.
                System.Diagnostics.Process.Start(startInfo);

                return;
            }

            Process current = Process.GetCurrentProcess();
            Process existProcess = Process.GetProcessesByName(current.ProcessName).Where(o => o.Id != current.Id).FirstOrDefault();

            if (existProcess == null)
            {
                Application.Run(new MainForm());
            }
            else
            {
                ShowWindowAsync(existProcess.MainWindowHandle, WS_SHOWNORMAL); //调用api函数，正常显示窗口
                SetForegroundWindow(existProcess.MainWindowHandle); //将窗口放置最前端。
            }
        }

        #region 注册默认打开方式
        
        /// <summary>
        /// 写入程序相关
        /// </summary>
        private static void WriteApp()
        {
            //设置程序打开路径
            string path = "\"C:\\Users\\Administrator\\Desktop\\Test\\TestWinForm\\bin\\Debug\\TestWinForm.exe\"";
            RegistryKey root = Registry.ClassesRoot;
            RegistryKey f1 = root.CreateSubKey("Form1");
            //设置注册表程序打开的项与值,其中SetValue("", path)代表设置默认值
            f1.CreateSubKey("shell").CreateSubKey("Open").CreateSubKey("Command").SetValue("", path);

            //设置程序图标
            string ico = "\"C:\\Users\\Administrator\\Desktop\\u7_original.ico\"";
            f1.CreateSubKey("DefaultIcon").SetValue("", ico);
        }

        /// <summary>
        /// 写关联文件
        /// </summary>
        private static void WriteRelate()
        {
            RegistryKey root = Registry.ClassesRoot;
            //设置打开程序
            root.CreateSubKey(".cws").CreateSubKey("OpenWithProgids").SetValue("Form1", "");
        }
        #endregion

        /*
        /// <summary>
        /// 设置文件关联
        /// </summary>
        /// <param name="p_Filename">程序的名称</param>
        /// <param name="p_FileTypeName">扩展名 .VRD </param>
        public static void SaveReg(string p_Filename,string p_FileTypeName)
        {
            RegistryKey _RegKey = Registry.ClassesRoot.OpenSubKey("", true);              //打开注册表

            RegistryKey _VRPkey = _RegKey.OpenSubKey(p_FileTypeName);
            if (_VRPkey != null) _RegKey.DeleteSubKey(p_FileTypeName, true);
            _RegKey.CreateSubKey(p_FileTypeName);
            _VRPkey = _RegKey.OpenSubKey(p_FileTypeName, true);
            _VRPkey.SetValue("", "Exec");

            _VRPkey = _RegKey.OpenSubKey("Exec", true);
            if (_VRPkey != null) _RegKey.DeleteSubKeyTree("Exec");         //如果等于空 就删除注册表DSKJIVR

            _RegKey.CreateSubKey("Exec");
            _VRPkey = _RegKey.OpenSubKey("Exec", true);
            _VRPkey.CreateSubKey("shell");
            _VRPkey = _VRPkey.OpenSubKey("shell", true);                      //写入必须路径
            _VRPkey.CreateSubKey("open");
            _VRPkey = _VRPkey.OpenSubKey("open", true);
            _VRPkey.CreateSubKey("command");
            _VRPkey = _VRPkey.OpenSubKey("command", true);
            string _PathString = "/"" + p_Filename + "/" /"%1/"";
            _VRPkey.SetValue("", _PathString);                                    //写入数据
        }
        */

    }
}
